package com.everest;
import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


class Missile {
	public static final int BULLET_SPEED = 10;
	public static final int WIDTH = 10;
	public static final int HEIGHT = 10;
	Direction dir;
	int x,y;
	private TankClient tc;
	private boolean live = true;
	
	private boolean good;
	
	private static Map<String,Image> imgs = new HashMap<String,Image>();
	private static Toolkit tk = Toolkit.getDefaultToolkit();
	private static Image[] missileImages = null;
	
	static {
		missileImages = new Image[] {
				tk.getImage(Explode.class.getClassLoader().getResource("images/missileL.gif")),
				tk.getImage(Explode.class.getClassLoader().getResource("images/missileLU.gif")),
				tk.getImage(Explode.class.getClassLoader().getResource("images/missileU.gif")),
				tk.getImage(Explode.class.getClassLoader().getResource("images/missileRU.gif")),
				tk.getImage(Explode.class.getClassLoader().getResource("images/missileR.gif")),
				tk.getImage(Explode.class.getClassLoader().getResource("images/missileRD.gif")),
				tk.getImage(Explode.class.getClassLoader().getResource("images/missileD.gif")),
				tk.getImage(Explode.class.getClassLoader().getResource("images/missileLD.gif")),
		   };
		imgs.put("L", missileImages[0]);
		imgs.put("LU", missileImages[1]);
		imgs.put("U", missileImages[6]);
		imgs.put("RU", missileImages[3]);
		imgs.put("R", missileImages[4]);
		imgs.put("RD", missileImages[5]);
		imgs.put("D", missileImages[2]);
		imgs.put("LD", missileImages[7]);
		
	}
	
	public Missile(int x, int y,Direction dir) {
		this.x = x;
		this.y = y;
		this.dir = dir;
	}
	
	public Missile(int x,int y,Direction dir,TankClient tc,boolean good) {
		this(x,y,dir);
		this.tc = tc;
		this.good = good;
	}
	
	public void draw(Graphics g) {
		if(!live) {
			tc.missiles.remove(this);
			return;
		}
		if(hitWall(tc.w1)) return;
		if(hitWall(tc.w2)) return;
		Color c = g.getColor();
		switch(dir) {
		case L:
			g.drawImage(imgs.get("L"), x,y, null);
			break;
		case LU:
			g.drawImage(imgs.get("LU"), x,y, null);
			break;
		case U:
			g.drawImage(imgs.get("U"), x,y, null);
			break;
		case RU:
			g.drawImage(imgs.get("RU"), x,y, null);
			break;
		case R:
			g.drawImage(imgs.get("R"), x,y, null);
			break;
		case RD:
			g.drawImage(imgs.get("RD"), x,y, null);
			break;
		case D:
			g.drawImage(imgs.get("D"), x,y, null);
			break;
		case LD:
			g.drawImage(imgs.get("LD"), x,y, null);
			break;
		
		}

		move();
		
	}
	
	public void move() {
		
		switch(dir) {
			case L:
				x -= BULLET_SPEED;	
				
				break;
			case LU:
				x -= BULLET_SPEED;
				y -= BULLET_SPEED;
				
				break;
			case U:
				y -= BULLET_SPEED;
				break;
			case RU:
				y -= BULLET_SPEED;
				x += BULLET_SPEED;
				break;
			case R:
				x += BULLET_SPEED;
				break;
			case RD:
				x += BULLET_SPEED;
				y += BULLET_SPEED;
				break;
			case D:
				y += BULLET_SPEED;
				break;
			case LD:
				y += BULLET_SPEED;
				x -= BULLET_SPEED;
				break;
		}
		if(x <0 || y < 0 || x > TankClient.GAME_WIDTH || y > TankClient.GAME_HEIGHT) {
			live = false;
		}
	}
	
	public Rectangle getRect() {
		return new Rectangle (x,y,WIDTH,HEIGHT);
	}
	
	public boolean hitTank(Tank t) {
		
		if(this.live && this.getRect().intersects(t.getRect()) && t.isLive() && t.isGood() != this.good) {
			if(t.isGood()) {
				t.setLife(t.getLife()-20);
				if(t.getLife() <= 0) {
					t.setLive(false);
					Tank.deadTimes++;
				}
				if(t.getLife() == 20) tc.b.setLive(true);
			} 
			else {t.setLive(false);}
			this.live = false;
			
			Explode e = new Explode(t.x + t.WIDTH/2,t.y + t.HEIGHT/2,tc);
			tc.explodes.add(e);
			return true;
		}
		return false;
	}

	public boolean hitTanks(List<Tank> enemyTanks) {
		for(int i=0;i<enemyTanks.size();i++) {
			Tank tk = (Tank)enemyTanks.get(i);
			if(hitTank(tk)){
				return true;
			}
		}
		return false;
	}
	
	public boolean hitWall(Wall w) {
		if(this.live && this.getRect().intersects(w.getRect())) {
			this.live = false;
			Explode e = new Explode(x + WIDTH/2,y + HEIGHT/2,tc);
			tc.explodes.add(e);
			return true;
		}
		return false;
	}
}